package com.xja.git.control;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xja.git.entity.*;
import com.github.pagehelper.PageInfo;
import com.xja.git.entity.Entry;
import com.xja.git.entity.Entry;
import com.xja.git.service.BlogUserService;
import com.xja.git.service.CategoryService;
import com.xja.git.service.ConmmentServcie;
import com.xja.git.service.EntryService;
import com.xja.git.utils.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.login.LoginContext;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：罗帅
 * 当前类所在包：com.xja.git.control
 * 创建时间：2022/8/17下午 5:15 星期三
 * 此类简介：
 */
@RestController
public class EntryController {
    @Autowired
    private BlogUserService blogUserService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private ConmmentServcie conmmentServcie;
    @Autowired
    private EntryService entryService;

    private Logger logger = LoggerFactory.getLogger(EntryController.class);

    public BlogUserService getBlogUserService() {
        return blogUserService;
    }

    public void setBlogUserService(BlogUserService blogUserService) {
        this.blogUserService = blogUserService;
    }

    public CategoryService getCategoryService() {
        return categoryService;
    }

    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    public ConmmentServcie getConmmentServcie() {
        return conmmentServcie;
    }

    public void setConmmentServcie(ConmmentServcie conmmentServcie) {
        this.conmmentServcie = conmmentServcie;
    }

    public EntryService getEntryService() {
        return entryService;
    }

    public void setEntryService(EntryService entryService) {
        this.entryService = entryService;
    }

    @PutMapping("Entry")
    public PageResult updateEntry(@RequestBody Entry entry){
        logger.info("修改日志");
        entryService.modifyEntry(entry);
        return PageResult.success("修改日志成功",null);
    };

    /**
     * 添加的方法
     * @param entry
     * @param session
     * @return
     */
    @PostMapping("add")
    public PageResult addEntry(@RequestBody Entry entry, HttpSession session) {
        logger.info("注册的方法" + entry);
        BlogUser blogUser = (BlogUser) session.getAttribute("loginUser");
        entry.setAuthor(blogUser.getId());
        entry.setPosttime(new Date());
        int n = entryService.addEntry(entry);
        if (n < 1) {
            return PageResult.none("添加失败");
        }
        return PageResult.success("添加成功",null);
    }



    //日志打印

    /**
    *
     * 分页查询所有的方法  日志表
    * */
    @GetMapping("queryAllEntry/{npage}")

    public PageResult QueryAll(@PathVariable("npage") Integer napge){
     logger.info("分页的方法");
     //给页数一个默认值  判断
          if (napge==null){
              napge=1;
          }
          //调用servcie的方法
        PageInfo<Entry> entryPageInfo = entryService.queryALL(napge);
          //封装到工具类中  方便前端调用
        System.out.println(entryPageInfo.getList());
        return  PageResult.success("查询成功",entryPageInfo);
    }
    /**
     *
     * 删除的方法
    * */
    @DeleteMapping("removeEntry/{id}")
    public PageResult DeleteById(@PathVariable("id") Integer id){
        logger.info("删除的方法 ");
        //调用方法
        int i = entryService.removeByPrimaryKey(id);
        //调用工具类 传给前台

        return PageResult.success("删除成功",null);
    }

    /**
     * 查询全部博客的方法
     * @return
     */
    @GetMapping("/findAllEnptyL/{npage}")
    public PageResult findAllEnptyL(@PathVariable Integer npage){
        logger.info("npage---->"+npage);
        if(npage==null || npage==0){
            npage=1;
        }

        PageInfo<Entry> pageInfo = entryService.findAllEnptyL(npage);
        return PageResult.success("查询成功",pageInfo);
    }

    /**
     * 向session域中赋值
     * @param entryId
     * @param session
     * @return
     */
    @GetMapping("/setId/{entryId}")
    public PageResult setId(@PathVariable Integer entryId, HttpSession session){
        logger.info("entryId---->"+entryId);
        session.setAttribute("entryId",entryId);
        return PageResult.success("赋值session域中成功",null);
    }

    /**
     * 查询单个的信息
     * @return
     */
    @GetMapping("/findEnptyLById")
    public PageResult findEnptyLById(HttpSession session){
        //查询个人信息
        Integer entryId = (Integer) session.getAttribute("entryId");
        Entry entry = entryService.findEnptyByIdL(entryId);

        //查询回复消息
        List<Conmment> conmmentList = conmmentServcie.findConmmentByEntryIdL(entryId);

        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("entry",entry);
        resultMap.put("conmmentList",conmmentList);
        //根据信息查看评论
        return PageResult.success("查询成功",resultMap);
    }

    @GetMapping("/setEntryId/{entryId}")
    public PageResult setEntryId(@PathVariable Integer entryId,HttpSession session){
        logger.info("entryId+++"+entryId);
        session.setAttribute("entryId2",entryId);
        return PageResult.success("赋值成功",null);
    }


    @GetMapping("/findEntryById2")
    public PageResult findEntryById2(HttpSession session){
        Integer entryId = (Integer) session.getAttribute("entryId2");
        logger.info("entryId---->"+entryId);
        Entry entry = entryService.findEnptyByIdL(entryId);
        return PageResult.success("查询成功",entry);
    }

}
